'use strict';

const Controller = require('egg').Controller;
let demo = [{
    id: 1,
    name: "1233",
    nickName: "赵奕航"
},
{
    id: 2,
    name: "1234",
    nickName: "李永超"
}]
class UserController extends Controller {
    async index() {
        this.ctx.throw(500, "出错")
        // let result = demo;
        //查询所有
        // let result=await this.app.model.User.findAll()
        //查询多个并计数
        let page = this.ctx.query.page ? parseInt(this.ctx.query.page) : 1;
        let result = await this.app.model.User.findAndCountAll({
            attributes: {
                exclude: ["password"]
            },
            order: [
                ['updated_at', 'DESC'],
                ['id', 'DESC']


            ],
            offset: (page - 1) * 2,
            limit: 2
        })

        this.ctx.body = {
            msg: "OK",
            query: this.ctx.query.page,
            data: result
        }
        this.ctx.status = 201
    }
    async read() {
        let id = this.ctx.params.id;
        this.ctx.throw(500, "出错")
        //查询单个id
        // let detail = await this.app.model.User.findByPk(parseInt(id));
        let Op = this.app.model.Sequelize.Op;
        let detail = await this.app.model.User.findOne({
            // where:{
            // id:parseInt(id),
            // sex:"女",
            // username:{
            //     [Op.like]:"%林%"
            // },
            // id:{
            //     [Op.gt]:6 

            // }

            // },
            // attributes:["id","username","sex"],
            attributes: {
                exclude: ["password"]
            },
            order: [
                ['updated_at', 'DESC'],
                ['id', 'DESC']


            ],
            limit: 3


        });
        // let result = demo.find(item => item.id === Number(this.ctx.params.id));
        if (!detail) {
            this.ctx.body = {
                msg: "OK",
                data: "用户不存在"
            }

        } else {
            this.ctx.body = {
                msg: "OK",
                data: detail
            }

        }
    }
    async createe() {
        this.ctx.body = 123
    }
    async create() {
        let params = this.ctx.request.body;
        this.ctx.validate({
            username: {
                type: "string",
                required: false,

                desc: "用户名称",
            },
            password: { type: "string", required: true, desc: "密码" },
            sex: { type: "string", required: true, defValue: "男", desc: "请输入性别" },
        });


        let res = await this.app.model.User.create(params)

        //新增多条

        // let res = await this.app.model.User.bulkCreate([
        //     {
        //         username: "赵奕航",
        //         password: "123",
        //         sex: "男"
        //     }, {
        //         username: "李永超",
        //         password: "123",
        //         sex: "男"
        //     }, {
        //         username: "邓海林",
        //         password: "123",
        //         sex: "男"
        //     },
        // ])

        this.ctx.body = res
    }
    async update() {
        let id = this.ctx.params.id ? parseInt(this.ctx.params.id) : 0;
        let data = await this.app.model.User.findByPk(id);
        if (!data) {
            return this.ctx.body = {
                msg: "400",
                data: "用户不存在"
            }
        }
        let params = this.ctx.request.body;

        let res = await data.update(params, {
            fields: ['username']
        });
        this.ctx.body = {
            msg: "200",
            data: res,
            params: params
        }
    }
    async destroy() {
        // let id = this.ctx.params.id ? parseInt(this.ctx.params.id) : 0;
        // let data = await this.app.model.User.findByPk(id);
        // if (!data) {
        //     return this.ctx.body = {
        //         msg: "fail",
        //         data: "该记录不存在"
        //     }
        // }
        // let res=await data.destroy();
        // this.ctx.body = {
        //     msg: "ok",
        //     data: res
        // }
        let Op = this.app.model.Sequelize.Op;
        let res = await this.app.model.User.destroy({
            where: {
                id: {
                    [Op.lte]: 13
                }
            }
        });
        this.ctx.body = {
            msg: "ok",
            data: res
        }

    }
}

module.exports = UserController;
